layui.define(["jquery", "layer", "table", "form", "okLayer"], function (exprots) {
   let $ = layui.jquery;
   let form = layui.form;
   let okLayer = layui.okLayer;
   let table = layui.table;
   let okPapper = {
      /**
       * 表单提交页面打开
       */
      ljOpenSubmit: function () {
         $("[target='ljOpenSubmit']").on("click", function () {
            let ljUrl = okPapper.modifyLjUrl($(this));
            if (!ljUrl) {return;}

            let title = $(this).attr("title") || '';
            let ljWidth = $(this).attr("ljWidth") || '90%';
            let ljHeight = $(this).attr("ljHeight") || '90%';
            $.ajax({
               url: ljUrl, type: 'GET', cache: false, data: {}, timeout: 1000 * 20, success: function (data) {
                  okLayer.closeAll();
                  layer.open({
                     type: 1, title: title,
                     shadeClose: false,
                     maxmin: true,
                     resize: true,
                     btn: ["确定", "取消"],
                     shade: [0.8, '#393D49'],
                     anim: okLayer.animChoose(),
                     area: [ljWidth, ljHeight],
                     content: data,
                     zIndex: layer.zIndex,
                     skin: okLayer.skinChoose(),
                     yes: function (index, layero) {
                        $(':focus').blur();
                        let layerForm = layero.find('.layui-form');
                        let layFilter = layerForm.attr('lay-filter');
                        let action = layerForm.attr('action');
                        let enctype = layerForm.attr('enctype');
                        let callback = layerForm.attr('callback');
                        let stop = form.doVerify(layFilter);
                        if (!stop) {
                           return;
                        }
                        // 表单提交
                        if ('multipart/form-data' === enctype) {
                           // 文件提交
                           let formData = new FormData(layerForm);
                        } else {
                           let subFormData = form.val(layFilter);
                           $.ajax({
                              type: 'POST', url: action, data: subFormData, dataType: "json", cache: false, success: function (data) {
                                 layer.close(index);
                                 if (!callback) {
                                    if (data.statusCode == '200') {
                                       okLayer.sweetSuccess("提交成功", data.message, function () {
                                          window.location.reload();
                                       });
                                    } else {
                                       okLayer.sweetError("提交失败", data.message);
                                    }
                                 } else {
                                    if (!$.isFunction(callback)) {
                                       let $callback = eval('(' + callback + ')');
                                       $callback(data);
                                    } else {
                                       callback(data);
                                    }
                                 }
                              }, error: function (xhr) {
                                 okLayer.sweetError("请求数据异常:" + xhr.status, xhr.responseText);
                                 layer.close(index);
                              }
                           });
                        }
                     }
                  });
               }, error: function (xhr) {
                  okLayer.sweetError("请求页面异常:" + xhr.status, xhr.responseText);
                  return;
               }
            });
         });
      },

      /**
       * 展示页面打开
       */
      ljOpenCon: function () {
         $("[target='ljOpenCon']").on("click", function () {
            let ljUrl = okPapper.modifyLjUrl($(this));
            if (!ljUrl) {return;}

            let title = $(this).attr("title") || '';
            let ljWidth = $(this).attr("ljWidth") || '90%';
            let ljHeight = $(this).attr("ljHeight") || '90%';
            $.ajax({
               url: ljUrl, type: 'GET', cache: false, data: {}, timeout: 1000 * 20, success: function (data) {
                  okLayer.closeAll();
                  okLayer.openCon(title, data, ljWidth, ljHeight);
               }, error: function (xhr) {
                  okLayer.sweetError("请求页面异常:" + xhr.status, xhr.responseText);
                  return;
               }
            });
         });
      },

      /**
       * adjx请求事件绑定
       */
      ljAjaxTodo: function () {
         $("[target='ljAjaxTodo']").on("click", function () {
            let ljUrl = okPapper.modifyLjUrl($(this));
            if (!ljUrl) {return;}

            let title = $(this).attr("title");
            let callback = $(this).attr("callback");
            okLayer.sweetConfirm(title, function (index) {
               $.ajax({
                  type: 'POST', url: ljUrl, dataType: "json", cache: false, success: function (data) {
                     if (!callback) {
                        if (data.statusCode == '200') {
                           okLayer.sweetSuccess("提交成功", data.message, function () {
                              window.location.reload();
                           });
                        } else {
                           okLayer.sweetError("提交失败", data.message);
                        }
                     } else {
                        if (!$.isFunction(callback)) {
                           let $callback = eval('(' + callback + ')');
                           $callback(data);
                        } else {
                           callback(data);
                        }
                     }
                  }, error: function (xhr) {
                     okLayer.sweetError("请求页面异常:" + xhr.status, xhr.responseText);
                     return;
                  }
               });
            });
         });
      },

      /**
       * 页面局部刷新
       */
      ljDivSerach: function () {
         $("[target='ljDivSerach']").on("click", function () {
            let rel = $(this).attr("rel");
            let ljUrl = okPapper.modifyLjUrl($(this));
            if (!ljUrl) {return;}

            let refTable = $(this).attr("refTable");
            ljUrl = okPapper.modifyLjUrl(ljUrl, refTable);
            $.ajax({
               url: ljUrl, type: 'GET', cache: false, data: {}, timeout: 1000 * 20, success: function (data) {
                  $(rel).html(data);
               }, error: function (xhr, status, error) {
                  okLayer.sweetError("请求页面异常:" + xhr.status, xhr.responseText);
               }
            });
         });
      },

      /**
       * 页面局部刷新
       */
      modifyLjUrl: function (obj) {
         let ljUrl = $(obj).attr("ljUrl");
         if (!ljUrl) {
            okLayer.sweetInfo("温馨提示", "请配置组件【ljUrl】值");
            return;
         }
         // 操作表格单条数据
         if (ljUrl.indexOf("{tid}") > 0) {
            let checkData = table.checkStatus($(obj).attr("refTable"));
            if (checkData.data.length <= 0) {
               okLayer.sweetError("未选择数据", "请选择一条你要操作的数据");
               return;
            }
            if (checkData.data.length > 1) {
               okLayer.sweetError("选择多条数据", "请选择一条你要操作的数据");
               return;
            }
            ljUrl = ljUrl.replace(new RegExp("\\{tid\\}", "g"), checkData.data[0].id);
            return ljUrl;
         }
         // 操作表格多条数据
         if (ljUrl.indexOf("{tids}") > 0) {
            let checkData = table.checkStatus($(obj).attr("refTable"));
            if (checkData.data.length <= 0) {
               okLayer.sweetError("未选择数据", "请选择一条你要操作的数据");
               return;
            }
            let ids = "";
            $.each(checkData.data, function (index, value) {
               if (index === 0) {
                  ids = value.id;
               } else {
                  ids = ids + "," + value.id;
               }
            });
            ljUrl = ljUrl.replace(new RegExp("\\{tids\\}", "g"), ids);
            return ljUrl;
         }
         return ljUrl;
      },

      /**
       * 初始化绑定事件
       */
      init: function () {
         this.ljOpenSubmit();
         this.ljOpenCon();
         this.ljAjaxTodo();
         this.ljDivSerach();

         $('.btnPageRefresh').on('click', function () {
            window.location.reload();
         });
         $('.btnPagerForm').on('click', function () {
            $("#pagerForm").submit();
         });
         $.fn.extend({
            layoutH: function ($refBox) {
               return this.each(function () {
                  let $this = $(this);
                  let iRefH = $(window).height();
                  let iLayoutH = parseInt($this.attr("layoutH"));
                  let iH = iRefH - iLayoutH > 50 ? iRefH - iLayoutH : 50;
                  $this.height(iH).css("overflow", "auto");
               });
            }
         });
         $("[layoutH]").layoutH();
         $(window).resize(function () {
            $("[layoutH]").layoutH();
         });
      },

      /**
       * 页面展示信息
       */
      pageConfig: function () {
         $(".SysPageConfigEditBtn").on("click", function () {
            let pageCode = $(".SysPageConfigEditBtn").attr("pageCode");
            let ljUrl = "/sys/page/config/edit?pageCode=" + pageCode;
            let fields = '';
            $(".SysPageConfigEditTable th").each(function () {
               let spf = $(this).attr("spf");
               if (fields == '') {
                  fields = fields + spf;
               } else {
                  fields = fields + "," + spf;
               }
            });
            ljUrl = ljUrl + "&fields=" + fields;
            $.ajax({
               url: ljUrl, type: 'GET', cache: false, data: {}, timeout: 1000 * 20, success: function (data) {
                  okLayer.closeAll();
                  okLayer.openCon('页面配置', data, '80%', '80%');
               }, error: function (xhr) {
                  okLayer.sweetError("请求页面异常:" + xhr.status, xhr.responseText);
                  return;
               }
            });
         });
      }
   };

   exprots("okPapper", okPapper);
});
